package controller;

import dao.OrderDao;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.*;
import tool.JsonResult;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller//控制器注解
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private SkillService skillService;

    @Autowired
    private StaffSkillService staffSkillService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;


    @RequestMapping("getorders")
    public String getorders(Order order, HttpSession session,Skill skill){
        System.out.println("dogetorders");
        System.out.println(order);
        System.out.println(skill);



        List<Order> orders = orderService.getOrders(order,skill);

        session.setAttribute("orders",orders);
        return "orderindex";
    }






    @RequestMapping("addorder")
    public String addorder(Order order, HttpSession session){
        System.out.println("addorder");
        System.out.println(order);
        Staff staff = new Staff();

        List<Staff> staffs = staffService.getstaffs(staff);

        List<Skill> skills = skillService.getSkills();

        session.setAttribute("skills",skills);
        session.setAttribute("staffs",staffs);
        return "addorder";
    }


    @RequestMapping("getpricebyskillid/{skillid}")
    @ResponseBody
    public JsonResult getpricebyskillid(@PathVariable Integer skillid,HttpSession session) {
        System.out.println("dogetpricebyskillid"+skillid);

        List<Skill> skills =(List<Skill>) session.getAttribute("skills");
        Integer price = 0;
        for (Skill skill : skills) {
            if (skill.getSkillid()==skillid){
                price=skill.getPrice();
            }
        }


        System.out.println("cg"+price);
        return new JsonResult(false, 10000, "获取成功1", price);
    }



    @RequestMapping("gettotalprice")
    @ResponseBody
    public JsonResult gettotalprice(Integer[] skillids,HttpSession session) {
        System.out.println("gettotalprice"+skillids);
/*

        for (Integer skillid : skillids) {
            System.out.println(skillid);
        }
*/


        List<Skill> skills =(List<Skill>) session.getAttribute("skills");
/*        for (Skill skill : skills) {
            System.out.println(skill.getSkillid()+"and"+skill.getPrice());
        }*/
        Integer totalprice = 0;
        for (Skill skill : skills) {
            for (Integer totalskillid : skillids) {
                if (skill.getSkillid()==totalskillid){
                    totalprice+=skill.getPrice();
                }

            }
        }


        return new JsonResult(false, 10000, "总价获取成功", totalprice);
    }



    @RequestMapping("doaddorder")
    @ResponseBody
    public JsonResult doaddorder(Order order, Integer[] skillids, Integer[] staffids,Integer userid ) {
        System.out.println("doaddstaff");
        User user = userService.getUserById(userid);
        if (user==null){
            return new JsonResult(false, 10001, "添加失败 用户不存在", null);
        }


        user.setUserid(userid);
        order.setUser(user);
        Integer addorder = orderService.addOrder(order);

        Integer orderid=order.getOrderid();
        System.out.println(orderid);
        Integer result = 0;


        if (addorder!=1){
            return new JsonResult(false, 10001, "添加失败 ", null);
        }



        if (skillids.length == staffids.length) {
            for (int i = 1; i < skillids.length; i++) {
            result+=orderDetailService.addOrderDetail(skillids[i],staffids[i],orderid);
            }


            if (result==skillids.length-1){
                return new JsonResult(false, 10000, "添加成功 ", null);
            }

            return new JsonResult(false, 10001, "添加失败 ", null);


        } else {
            return new JsonResult(false, 10001, "添加失败 ", null);
        }


    }

    @RequestMapping("delorder/{orderid}")
    @ResponseBody
    public JsonResult delstaff(@PathVariable Integer orderid) {
        System.out.println("orderdelid" + orderid);

        Integer result = orderService.delOrder(orderid);
        Integer result2 = orderDetailService.delOrderDetail(orderid);
        System.out.println(result2);

        System.out.println(result);
        if (result > 0) {
            return new JsonResult(true, 10000, "删除成功", null);
        } else {
            return new JsonResult(false, 10001, "删除失败", null);

        }
    }


    @RequestMapping("updateorder/{orderid}")
    public String updateorder(@PathVariable Integer orderid,HttpSession session) {
        System.out.println("updateorder"+orderid);




        Order updateorder=orderService.getOrderById(orderid);
        System.out.println(updateorder);
        Staff staff = new Staff();

        List<Staff> staffs = staffService.getstaffs(staff);

        List<Skill> skills = skillService.getSkills();

        System.out.println(staffs);
        System.out.println(skills);


        session.setAttribute("skills",skills);
        session.setAttribute("staffs",staffs);



        session.setAttribute("updateorder",updateorder);
        return "updateorder";


    }


    @RequestMapping("doupdateaddorder")
    @ResponseBody
    public JsonResult doupdateaddorder(Order order, Integer[] skillids, Integer[] staffids,Integer userid ) {
        System.out.println("doupdateaddorder");

        User user = userService.getUserById(userid);
        if (user==null){
            return new JsonResult(false, 10001, "修改失败 用户不存在", null);
        }

        user.setUserid(userid);
        order.setUser(user);
        System.out.println(order);
        Integer addorder = orderService.updateOrder(order);

        Integer orderid=order.getOrderid();
        System.out.println(orderid);
        Integer delresult=orderDetailService.delOrderDetail(orderid);
        Integer result=0;

        if (skillids.length == staffids.length) {
            for (int i = 1; i < skillids.length; i++) {
                result+=orderDetailService.addOrderDetail(skillids[i],staffids[i],orderid);
            }


            if (result==skillids.length-1){
                return new JsonResult(false, 10000, "修改成功 ", null);
            }

            return new JsonResult(false, 10001, "修改失败 ", null);


        } else {
            return new JsonResult(false, 10001, "修改失败 ", null);
        }

    }


    @RequestMapping("getstaffsbyskillid/{skillid}")
    @ResponseBody
    public JsonResult getstaffsbyskillid(@PathVariable Integer skillid, HttpSession session ) {
        System.out.println("getstaffsbyskillid"+skillid);
        List<Staff> dystaffs=staffSkillService.getSftaffsBySkillId(skillid);

        Staff staff = new Staff();

        List<Staff> staffs = staffService.getstaffs(staff);

        session.setAttribute("staffs", staffs);

        if (dystaffs!=null){
            System.out.println(dystaffs);

            return new JsonResult(true, 10000, "获得动态职工 ", dystaffs);
        }



        return new JsonResult(false, 10001, "获得动态职工失败 ", null);
    }


}
